import {
  createRouter,
  createWebHashHistory
} from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { message } from 'ant-design-vue';

const routes = [
  {
    path: '/',
    redirect: 'login'
  },
  {
    path: '/login',
    name: 'Login',
    meta: { title: '登录'},
    component: () => import('../views/Login.vue')
  },
  {
    path: '/find_passwd',
    name: 'Passwd',
    meta: { title: '找回密码'},
    component: () => import('../views/Passwd.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    children:[
      {
        path:'/home',
        redirect:'/home/Index/ShopIndex'
      },
      // 主页 =========================================================================
      {
        path: 'Index/ShopIndex',
        name: 'ShopIndex',
        meta: { title: '编辑店铺主页'},
        component: () => import('../views/home/index/ShopIndex.vue'),
      },
      {
        path: 'Index/platform',
        name: 'platform',
        meta: { title: '平台数据'},
        component: () => import('../views/home/index/platform.vue'),
      },
      {
        path: 'Index/shop',
        name: 'shop',
        meta: { title: '店铺数据'},
        component: () => import('../views/home/index/shop.vue'),
      },
      // 店铺 =========================================================================
      {
        path: 'Store/StoreList',
        name: 'StoreList',
        meta: { title: '店铺列表'},
        component: () => import('../views/home/store/StoreList.vue'),
      },
      {
        path: 'Store/StoreAdd',
        name: 'StoreAdd',
        meta: { title: '店铺添加'},
        component: () => import('../views/home/store/StoreAdd.vue'),
      },
      {
        path: 'Store/StoreApply',
        name: 'StoreApply',
        meta: { title: '店铺申请'},
        component: () => import('../views/home/store/StoreApply.vue'),
      },
      // 商品 =========================================================================
      {
        path: 'Product/ProductList',
        name: 'ProductList',
        meta: { title: '商品列表'},
        component: () => import('../views/home/product/ProductList.vue'),
      },
      {
        path: 'Product/ProductClass',
        name: 'ProductClass',
        meta: { title: '商品分类'},
        component: () => import('../views/home/product/ProductClass.vue'),
      },
      {
        path: 'Product/ProductAdd',
        name: 'ProductAdd',
        meta: { title: '商品添加'},
        component: () => import('../views/home/product/ProductAdd.vue'),
      },
      // 订单 =========================================================================
      {
        path: 'Order/OrderList',
        name: 'OrderList',
        meta: { title: '订单列表'},
        component: () => import('../views/home/order/OrderList.vue'),
      },
      {
        path: 'Order/OrderInfo',
        name: 'OrderInfo',
        meta: { title: '订单信息'},
        component: () => import('../views/home/order/OrderInfo.vue'),
      },
      {
        path: 'Order/OrderRefund',
        name: 'OrderRefund',
        meta: { title: '退货申请'},
        component: () => import('../views/home/order/OrderRefund.vue'),
      },
      {
        path: 'Order/Deliver',
        name: 'Deliver',
        meta: { title: '商品发货'},
        component: () => import('../views/home/order/Deliver.vue'),
      },
      {
        path: 'Order/RefundInfo',
        name: 'RefundInfo',
        meta: { title: '退货详情'},
        component: () => import('../views/home/order/RefundInfo.vue'),
      },
      //营销 =========================================================================
      {
        path: 'Marketing/Carousel',
        name: 'Carousel',
        meta: { title: '轮播图'},
        component: () => import('../views/home/marketing/Carousel.vue'),
      },
      {
        path: 'Marketing/Reduction',
        name: 'Reduction',
        meta: { title: '降价好物'},
        component: () => import('../views/home/marketing/Reduction.vue'),
      },
      {
        path: 'Marketing/ReductionApply',
        name: 'ReductionApply',
        meta: { title: '申请降价好物'},
        component: () => import('../views/home/marketing/ReductionApply.vue'),
      },
      {
        path: 'Marketing/Recmmend',
        name: 'Recmmend',
        meta: { title: '推荐好物'},
        component: () => import('../views/home/marketing/Recmmend.vue'),
      },
      {
        path: 'Marketing/RecmmendApply',
        name: 'RecmmendApply',
        meta: { title: '申请推荐好物'},
        component: () => import('../views/home/marketing/RecmmendApply.vue'),
      },
      //用户 =========================================================================
      {
        path: 'Customer/CustomerList',
        name: 'CustomerList',
        meta: { title: '用户列表'},
        component: () => import('../views/home/customer/CustomerList.vue'),
      },
      //通知 =========================================================================
      {
        path: 'Notification/Release',
        name: 'Release',
        meta: { title: '发布通知'},
        component: () => import('../views/home/notification/Release.vue'),
      },
      {
        path: 'Notification/SysNotificationN',
        name: 'SysNotificationN',
        meta: { title: '系统通知'},
        component: () => import('../views/home/notification/SysNotification.vue'),
      },
      //消息 =========================================================================
      {
        path: 'Message/Service',
        name: 'Service',
        meta: { title: '客服'},
        component: () => import('../views/home/message/Service.vue'),
      },
      {
        path: 'Message/SysNotificationM',
        name: 'SysNotificationM',
        meta: { title: '系统通知'},
        component: () => import('../views/home/message/SysNotification.vue')
      },
       //权限管理 =========================================================================
       {
        path: 'Authority/AdminList',
        name: 'AdminList',
        meta: { title: '管理员列表'},
        component: () => import('../views/home/authority/AdminList.vue'),
      },
      {
        path: 'Authority/StaffList',
        name: 'StaffList',
        meta: { title: '员工列表'},
        component: () => import('../views/home/authority/StaffList.vue'),
      },
      {
        path: 'Authority/RoleList',
        name: 'RoleList',
        meta: { title: '角色列表'},
        component: () => import('../views/home/authority/RoleList.vue'),
      },
      {
        path: 'Authority/MenuList',
        name: 'MenuList',
        meta: { title: '菜单列表'},
        component: () => import('../views/home/authority/MenuList.vue'),
      },
      {
        path: 'Authority/Resource',
        name: 'Resource',
        meta: { title: '资源列表'},
        component: () => import('../views/home/authority/Resource.vue'),
      },



    ]
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 给路由设置守卫函数
router.beforeEach((to, form, next) => {
  if(to.meta.title){
    document.title = to.meta.title
  }
  NProgress.start()


  if (to.path === '/'||to.path === '/login'||to.path==='/find_passwd') return next()
  
   const token= sessionStorage.getItem("access-token")
   
     if(!token){
      message.error('您未登录！请先登录');
      return next('/login')
     } 
     next()


  // to表示将要访问的路径
  // form表示从那个页面跳转而来
  // next表示允许跳转到指定位置
  /////if (to.path === '/login') return next()
  // 获取用户本地的token, 如果token不存在则跳转到登录页
  //// const tokenStr = window.sessionStorage.getItem('token')
  //// if (!tokenStr) return next('/login')
  //// next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router